package user.ucm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import user.view.MainView;

public class UCSearchLine {

    private MainView mv;

    public UCSearchLine(MainView mv) {
        this.mv = mv;
    }

    public int run() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cp3120?user=root&password=root");
            con.setCatalog("cp3120");

            Statement statement = con.createStatement();
            String search = "SELECT * FROM airinfo WHERE AirlineName='" +
                    this.mv.getAirVModel().getSearch_TextField().getText() + "'";
            System.out.println(search);

            ResultSet test = statement.executeQuery(search);
            if (!test.first()) {
                this.mv.getAirVModel().getAirName_TextField().setText("Not Found");
                this.mv.getAirVModel().getDestination_TextField().setText(test.getString("Null"));
                this.mv.getAirVModel().getSetOut_TextField().setText(test.getString("Null"));
                this.mv.getAirVModel().getPrice_TextField().setText(test.getString("Null"));
            } else {
                test.beforeFirst();
                test.next();
                this.mv.getAirVModel().getDestination_TextField().setText(test.getString("AirlineDestine"));
                this.mv.getAirVModel().getSetOut_TextField().setText(test.getString("AirlineTime"));
                this.mv.getAirVModel().getPrice_TextField().setText(test.getString("AirlinePrice"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return 2;
        }
        return 0;
    }
}
